package lp.xian.equipment.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import lp.xian.equipment.domain.Finance;
import lp.xian.equipment.domain.Upkeep;
import lp.xian.equipment.mapper.FinanceMapper;
import lp.xian.equipment.mapper.UpkeepMapper;
import lp.xian.equipment.service.IUpkeepService;
import lp.xian.equipment.vo.Upkeepvo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 设备保养Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-01
 */
@Service
public class UpkeepServiceImpl implements IUpkeepService
{
    @Autowired
    private UpkeepMapper upkeepMapper;
    @Autowired
    private FinanceMapper financeMapper;

    /**
     * 查询设备保养
     *
     * @param upkeepId 设备保养主键
     * @return 设备保养
     */
    @Override
    public Upkeep selectUpkeepByUpkeepId(Integer upkeepId)
    {
        return upkeepMapper.selectUpkeepByUpkeepId(upkeepId);
    }

    /**
     * 查询设备保养列表
     *
     * @param upkeep 设备保养
     * @return 设备保养
     */
    @Override
    public List<Upkeep> selectUpkeepList(Upkeep upkeep)
    {
        return upkeepMapper.selectUpkeepList(upkeep);
    }

    /**
     * 新增设备保养
     *
     * @param upkeep 设备保养
     * @return 结果
     */
    @Override
    public int insertUpkeep(Upkeep upkeep)
    {
        upkeep.setCreateTime(DateUtils.getNowDate());
        return upkeepMapper.insertUpkeep(upkeep);
    }

    /**
     * 修改设备保养
     *
     * @param upkeep 设备保养
     * @return 结果
     */
    @Override
    public int updateUpkeep(Upkeep upkeep)
    {
        upkeep.setUpdateTime(DateUtils.getNowDate());
        return upkeepMapper.updateUpkeep(upkeep);
    }

    /**
     * 批量删除设备保养
     *
     * @param upkeepIds 需要删除的设备保养主键
     * @return 结果
     */
    @Override
    public int deleteUpkeepByUpkeepIds(Integer[] upkeepIds)
    {
        return upkeepMapper.deleteUpkeepByUpkeepIds(upkeepIds);
    }

    /**
     * 删除设备保养信息
     *
     * @param upkeepId 设备保养主键
     * @return 结果
     */
    @Override
    public int deleteUpkeepByUpkeepId(Integer upkeepId)
    {
        return upkeepMapper.deleteUpkeepByUpkeepId(upkeepId);
    }

    @Override
    public int approverUpkeep(Upkeepvo upkeepvo) {
        //审批后数据需要提交到财务 收入支出
        Upkeep upkeep=new Upkeep();
        upkeep.setUpkeepId(upkeepvo.getUpkeepId());
        upkeep.setApproverOpinion(upkeepvo.getApproverOpinion());
        upkeep.setAuditStatus(upkeepvo.getAuditStatus());
        upkeep.setApproverTime(new Date());
        upkeep.setApproverPerson(SecurityUtils.getUsername());
        int num=upkeepMapper.updateUpkeep(upkeep);
        if (num>0) {
            Upkeep upkeep1 = upkeepMapper.selectUpkeepByUpkeepId(upkeepvo.getUpkeepId());
            Finance money = financeMapper.getFinanceMoney();
            if (upkeep1.getAuditStatus().intValue() == 2) {
                Finance finance = new Finance();
                finance.setFinanceName("【保养】" + upkeep1.getUpkeepName());
                finance.setFinanceMoney(upkeep1.getUpkeepPrice());
                finance.setFinanceBalance(money.getFinanceBalance());
                finance.setFinanceStatus(0);
                finance.setAuditStatus(0);
                finance.setCreateTime(new Date());
                finance.setCreateBy(SecurityUtils.getUsername());
                finance.setFinanceBalance(money.getFinanceBalance());
                return financeMapper.insertFinance(finance);
            }
        }
        return 1;

    }

    @Override
    public int auditUpkeep(Upkeepvo upkeepvo) {
        Upkeep upkeep=new Upkeep();
        upkeep.setUpkeepId(upkeepvo.getUpkeepId());
        upkeep.setAuditOpinion(upkeepvo.getAuditOpinion());
        upkeep.setAuditStatus(upkeepvo.getAuditStatus());
        upkeep.setAuditTime(new Date());
        upkeep.setAuditPerson(SecurityUtils.getUsername());
        return upkeepMapper.updateUpkeep(upkeep);
    }

    @Override
    public Upkeep selectUpkeepLists(Integer upkeepId) {
        return upkeepMapper.selectUpkeepLists(upkeepId);
    }
}
